// pages/edit/edit.ts
var { request } = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      "name": "",
      "plan": "",
      "assignee": "",
      "assigneeName": "",
      "finishTime": "",
      "organizer": "",
      "level": "",
	    "project": ''
    },
    projectIndex:0,
    addFlag: '',
    startDate: new Date().getTime(),
    assignList: [] as any,
    assignIndex: '',
    projectList:[] as any,
    projectIds:[],
    levelList: [{
      id: 0,
      name: '正常'
    }, {
      id: 1,
      name: '紧急'
    }],
    levelIndex: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option: any) {
    this.data.addFlag = option.addFlag
    if(option.todo){
      let todo = JSON.parse(option.todo)
      this.setData({
        formData: todo
      })
    }
    this.initAssignList(this.data.formData.project)
    this.initProjectList()
    // setTimeout(()=>{
    // },100)
  },
  initAssignList(project = '') {
    request(`/admin/employee/page?page=1&pageSize=100&project=${project}`, {
    }).then((res: any) => {
      if (res.data.code == 1) {
        let list = res.data.data.records
        this.setData({
          assignList: list.filter((item: any) => 
          item.processStatus == 1&&item.status==1),
        })
        console.log(this.data.assignList, 'assignList');
      }
    })
  },
  initProjectList(){
    request('/admin/project/list', {
      method:'POST',
      data:{}
    }).then((res: any) => {
      if (res.data.code == 1) {
        this.setData({
          projectList: res.data.data,
          projectIndex:res.data.data.map((item:any)=>{
            return item.id
          }).indexOf(this.data.formData.project)
        })
        console.log(this.data.projectList,this.data.projectIndex,'projectList');
      }
    })
  },
  formSubmit(e:any) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    // this.data.formData = e.detail.value;
    for (let key in e.detail.value) {
      this.data.formData[key] = e.detail.value[key]
    }
    let formData = this.data.formData
    console.log(formData, 'formSubmit');
    if (!formData.name || !formData.plan || !formData.finishTime || formData.assignee==='' || !formData.organizer || !formData.level) {
      console.log(!formData.name,!formData.plan,!formData.finishTime,
        formData.assignee=='', !formData.organizer,!formData.level);
      wx.showToast({
        title: '请将任务信息填写完整',
        icon: 'none',
      });
      return;
    }
    formData.status = 0
    request(this.data.addFlag=='1'?'/admin/task/add':'/admin/task/update', {
      data: formData,
      method: 'POST'
    }).then((res: any) => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000,
        })
        // this.formReset(1)
        wx.navigateBack({
          delta: 2
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  formReset(e:any) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    wx.navigateBack({
      delta: 2
    })
  },
  bindDateChange: function (e: any) {
    console.log('date发送选择改变，携带值为', e.detail.value)
    this.setData({
      'formData.finishTime': e.detail.value
    })
  },
  projectChange(e:any){
    console.log(e.detail.value,'projectChange');
    this.setData({
      'formData.project': this.data.projectList[e.detail.value].id,
      'formData.projectName': this.data.projectList[e.detail.value].name,
      projectIndex:e.detail.value
    })
    this.initAssignList(this.data.formData.project)
    console.log(this.data.formData,'formData.project');
  },
  assignChange: function (e:any) {
    console.log('assign发送选择改变，携带值为', e.detail.value)
    this.setData({
      assignIndex: e.detail.value
    })
    this.setData({
      'formData.assignee':e.detail.value,
      'formData.assigneeName':this.data.assignList[e.detail.value].name
    })
    console.log(this.data.formData.assigneeName, 'assigneeName');
  },
  levelChange: function (e:any) {
    console.log('level发送选择改变，携带值为', e.detail.value)
    this.setData({
      'formData.level': e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})